package cn.gwm.flink.streaming.beans.termalRunaway;

import cn.gwm.flink.streaming.constant.DefaultConstant;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * 热失控数据处理临时类
 * @author GW00283474
 */
@Data
@Slf4j
public class WaringTermalDwmWithStragegyPhev implements Serializable {
    /**
     * 车辆代码
     */
    public String deviceId;

    /**
     * 时间序列ID
     */
    public Long item_time;

    /**
     * 电池热失控
     */
    public Integer BMS_BattHeatRunaway;
    /**
     * 电池模组11温度
     */
    public Integer BMS_BattModuleTe_11;
    /**
     * 电池模组12温度
     */
    public Integer BMS_BattModuleTe_12;
    /**
     * 电池模组13温度
     */
    public Integer BMS_BattModuleTe_13 ;
    /**
     * 电池模组21温度
     */
    public Integer BMS_BattModuleTe_21 ;
    /**
     * 电池模组22温度
     */
    public Integer BMS_BattModuleTe_22 ;
    /**
     * 电池模组23温度
     */
    public Integer BMS_BattModuleTe_23 ;
    /**
     * 电池模组31温度
     */
    public Integer BMS_BattModuleTe_31 ;
    /**
     * 电池模组32温度
     */
    public Integer BMS_BattModuleTe_32 ;
    /**
     * 电池模组33温度
     */
    public Integer BMS_BattModuleTe_33 ;
    /**
     * 电池模组41温度
     */
    public Integer BMS_BattModuleTe_41 ;
    /**
     * 电池模组42温度
     */
    public Integer BMS_BattModuleTe_42 ;
    /**
     * 电池模组43温度
     */
    public Integer BMS_BattModuleTe_43 ;
    /**
     * 电池模组51温度
     */
    public Integer BMS_BattModuleTe_51 ;
    /**
     * 电池模组52温度
     */
    public Integer BMS_BattModuleTe_52 ;
    /**
     * 电池模组53温度
     */
    public Integer BMS_BattModuleTe_53 ;
    /**
     * 电池模组61温度
     */
    public Integer BMS_BattModuleTe_61 ;
    /**
     * 电池模组62温度
     */
    public Integer BMS_BattModuleTe_62 ;
    /**
     * 电池模组63温度
     */
    public Integer BMS_BattModuleTe_63 ;
    /**
     * 电池模组71温度
     */
    public Integer BMS_BattModuleTe_71 ;
    /**
     * 电池模组72温度
     */
    public Integer BMS_BattModuleTe_72 ;
    /**
     * 电池模组81温度
     */
    public Integer BMS_BattModuleTe_81 ;
    /**
     * 电池模组82温度
     */
    public Integer BMS_BattModuleTe_82 ;
    /**
     * 电池模组91温度
     */
    public Integer BMS_BattModuleTe_91 ;
    /**
     * 电池模组92温度
     */
    public Integer BMS_BattModuleTe_92 ;

    /**
     * 最大模组温度
     */
    public Integer BMS_BattTempMax;
    /**
     * 最小模组温度
     */
    public Integer BMS_BattTempMin ;

    /**
     * 单体最大电压
     */
    public BigDecimal BMS_CellMaxVolt;
    /**
     * 单体最小电压
     */
    public BigDecimal BMS_CellVoltMin;
    /**
     * 单体1电压
     */
    public BigDecimal BMS_SignleCellVolt_1;
    /**
     * 单体10电压
     */
    public BigDecimal BMS_SignleCellVolt_10;
    /**
     * 单体11电压
     */
    public BigDecimal BMS_SignleCellVolt_11;
    /**
     * 单体12电压
     */
    public BigDecimal BMS_SignleCellVolt_12;
    /**
     * 单体13电压
     */
    public BigDecimal BMS_SignleCellVolt_13;
    /**
     * 单体14电压
     */
    public BigDecimal BMS_SignleCellVolt_14;
    /**
     * 单体15电压
     */
    public BigDecimal BMS_SignleCellVolt_15;
    /**
     * 单体16电压
     */
    public BigDecimal BMS_SignleCellVolt_16;
    /**
     * 单体17电压
     */
    public BigDecimal BMS_SignleCellVolt_17;
    /**
     * 单体18电压
     */
    public BigDecimal BMS_SignleCellVolt_18;
    /**
     * 单体19电压
     */
    public BigDecimal BMS_SignleCellVolt_19;
    /**
     * 单体2电压
     */
    public BigDecimal BMS_SignleCellVolt_2;
    /**
     * 单体20电压
     */
    public BigDecimal BMS_SignleCellVolt_20;
    /**
     * 单体21电压
     */
    public BigDecimal BMS_SignleCellVolt_21;
    /**
     * 单体22电压
     */
    public BigDecimal BMS_SignleCellVolt_22;
    /**
     * 单体23电压
     */
    public BigDecimal BMS_SignleCellVolt_23;
    /**
     * 单体24电压
     */
    public BigDecimal BMS_SignleCellVolt_24;
    /**
     * 单体25电压
     */
    public BigDecimal BMS_SignleCellVolt_25;
    /**
     * 单体26电压
     */
    public BigDecimal BMS_SignleCellVolt_26;
    /**
     * 单体27电压
     */
    public BigDecimal BMS_SignleCellVolt_27;
    /**
     * 单体28电压
     */
    public BigDecimal BMS_SignleCellVolt_28;
    /**
     * 单体29电压
     */
    public BigDecimal BMS_SignleCellVolt_29;
    /**
     * 单体3电压
     */
    public BigDecimal BMS_SignleCellVolt_3;

    /**
     * 单体30电压
     */
    public BigDecimal BMS_SignleCellVolt_30;
    /**
     * 单体31电压
     */
    public BigDecimal BMS_SignleCellVolt_31;
    /**
     * 单体32电压
     */
    public BigDecimal BMS_SignleCellVolt_32;
    /**
     * 单体33电压
     */
    public BigDecimal BMS_SignleCellVolt_33;
    /**
     * 单体34电压
     */
    public BigDecimal BMS_SignleCellVolt_34;
    /**
     * 单体35电压
     */
    public BigDecimal BMS_SignleCellVolt_35;
    /**
     * 单体36电压
     */
    public BigDecimal BMS_SignleCellVolt_36;
    /**
     * 单体37电压
     */
    public BigDecimal BMS_SignleCellVolt_37;
    /**
     * 单体38电压
     */
    public BigDecimal BMS_SignleCellVolt_38;
    /**
     * 单体39电压
     */
    public BigDecimal BMS_SignleCellVolt_39;
    /**
     * 单体4电压
     */
    public BigDecimal BMS_SignleCellVolt_4;
    /**
     * 单体40电压
     */
    public BigDecimal BMS_SignleCellVolt_40;
    /**
     * 单体41电压
     */
    public BigDecimal BMS_SignleCellVolt_41;
    /**
     * 单体42电压
     */
    public BigDecimal BMS_SignleCellVolt_42;
    /**
     * 单体43电压
     */
    public BigDecimal BMS_SignleCellVolt_43;
    /**
     * 单体44电压
     */
    public BigDecimal BMS_SignleCellVolt_44;
    /**
     * 单体45电压
     */
    public BigDecimal BMS_SignleCellVolt_45;
    /**
     * 单体46电压
     */
    public BigDecimal BMS_SignleCellVolt_46;
    /**
     * 单体47电压
     */
    public BigDecimal BMS_SignleCellVolt_47;
    /**
     * 单体48电压
     */
    public BigDecimal BMS_SignleCellVolt_48;
    /**
     * 单体49电压
     */
    public BigDecimal BMS_SignleCellVolt_49;
    /**
     * 单体5电压
     */
    public BigDecimal BMS_SignleCellVolt_5;
    /**
     * 单体50电压
     */
    public BigDecimal BMS_SignleCellVolt_50;
    /**
     * 单体51电压
     */
    public BigDecimal BMS_SignleCellVolt_51;
    /**
     * 单体52电压
     */
    public BigDecimal BMS_SignleCellVolt_52;
    /**
     * 单体53电压
     */
    public BigDecimal BMS_SignleCellVolt_53;
    /**
     * 单体54电压
     */
    public BigDecimal BMS_SignleCellVolt_54;
    /**
     * 单体55电压
     */
    public BigDecimal BMS_SignleCellVolt_55;
    /**
     * 单体56电压
     */
    public BigDecimal BMS_SignleCellVolt_56;
    /**
     * 单体57电压
     */
    public BigDecimal BMS_SignleCellVolt_57;
    /**
     * 单体58电压
     */
    public BigDecimal BMS_SignleCellVolt_58;
    /**
     * 单体59电压
     */
    public BigDecimal BMS_SignleCellVolt_59;
    /**
     * 单体6电压
     */
    public BigDecimal BMS_SignleCellVolt_6;
    /**
     * 单体60电压
     */
    public BigDecimal BMS_SignleCellVolt_60;
    /**
     * 单体61电压
     */
    public BigDecimal BMS_SignleCellVolt_61;
    /**
     * 单体62电压
     */
    public BigDecimal BMS_SignleCellVolt_62;
    /**
     * 单体63电压
     */
    public BigDecimal BMS_SignleCellVolt_63;
    /**
     * 单体64电压
     */
    public BigDecimal BMS_SignleCellVolt_64;
    /**
     * 单体65电压
     */
    public BigDecimal BMS_SignleCellVolt_65;
    /**
     * 单体66电压
     */
    public BigDecimal BMS_SignleCellVolt_66;
    /**
     * 单体67电压
     */
    public BigDecimal BMS_SignleCellVolt_67;
    /**
     * 单体68电压
     */
    public BigDecimal BMS_SignleCellVolt_68;
    /**
     * 单体69电压
     */
    public BigDecimal BMS_SignleCellVolt_69;
    /**
     * 单体7电压
     */
    public BigDecimal BMS_SignleCellVolt_7;
    /**
     * 单体70电压
     */
    public BigDecimal BMS_SignleCellVolt_70;
    /**
     * 单体71电压
     */
    public BigDecimal BMS_SignleCellVolt_71;
    /**
     * 单体72电压
     */
    public BigDecimal BMS_SignleCellVolt_72;
    /**
     * 单体73电压
     */
    public BigDecimal BMS_SignleCellVolt_73;
    /**
     * 单体74电压
     */
    public BigDecimal BMS_SignleCellVolt_74;
    /**
     * 单体75电压
     */
    public BigDecimal BMS_SignleCellVolt_75;
    /**
     * 单体76电压
     */
    public BigDecimal BMS_SignleCellVolt_76;
    /**
     * 单体77电压
     */
    public BigDecimal BMS_SignleCellVolt_77;
    /**
     * 单体78电压
     */
    public BigDecimal BMS_SignleCellVolt_78;
    /**
     * 单体79电压
     */
    public BigDecimal BMS_SignleCellVolt_79;
    /**
     * 单体8电压
     */
    public BigDecimal BMS_SignleCellVolt_8;
    /**
     * 单体80电压
     */
    public BigDecimal BMS_SignleCellVolt_80;
    /**
     * 单体81电压
     */
    public BigDecimal BMS_SignleCellVolt_81;
    /**
     * 单体82电压
     */
    public BigDecimal BMS_SignleCellVolt_82;
    /**
     * 单体83电压
     */
    public BigDecimal BMS_SignleCellVolt_83;
    /**
     * 单体84电压
     */
    public BigDecimal BMS_SignleCellVolt_84;
    /**
     * 单体85电压
     */
    public BigDecimal BMS_SignleCellVolt_85;
    /**
     * 单体86电压
     */
    public BigDecimal BMS_SignleCellVolt_86;
    /**
     * 单体87电压
     */
    public BigDecimal BMS_SignleCellVolt_87;
    /**
     * 单体88电压
     */
    public BigDecimal BMS_SignleCellVolt_88;
    /**
     * 单体89电压
     */
    public BigDecimal BMS_SignleCellVolt_89;
    /**
     * 单体9电压
     */
    public BigDecimal BMS_SignleCellVolt_9;
    /**
     * 单体90电压
     */
    public BigDecimal BMS_SignleCellVolt_90;
    /**
     * 单体91电压
     */
    public BigDecimal BMS_SignleCellVolt_91;
    /**
     * 单体92电压
     */
    public BigDecimal BMS_SignleCellVolt_92;
    /**
     * 单体93电压
     */
    public BigDecimal BMS_SignleCellVolt_93;
    /**
     * 单体94电压
     */
    public BigDecimal BMS_SignleCellVolt_94;
    /**
     * 单体95电压
     */
    public BigDecimal BMS_SignleCellVolt_95;
    /**
     * 单体96电压
     */
    public BigDecimal BMS_SignleCellVolt_96;
    /**
     * 单体97电压
     */
    public BigDecimal BMS_SingleCellVolt97;
    /**
     * 单体98电压
     */
    public BigDecimal BMS_SingleCellVolt98;
    /**
     * 单体99电压
     */
    public BigDecimal BMS_SingleCellVolt99;
    /**
     * 单体100电压
     */
    public BigDecimal BMS_SingleCellVolt100;
    /**
     * 单体101电压
     */
    public BigDecimal BMS_SingleCellVolt101;
    /**
     * 单体102电压
     */
    public BigDecimal BMS_SingleCellVolt102;
    /**
     * 单体103电压
     */
    public BigDecimal BMS_SingleCellVolt103;
    /**
     * 单体104电压
     */
    public BigDecimal BMS_SingleCellVolt104;
    /**
     * 单体105电压
     */
    public BigDecimal BMS_SingleCellVolt105;
    /**
     * 单体106电压
     */
    public BigDecimal BMS_SingleCellVolt106;
    /**
     * 单体107电压
     */
    public BigDecimal BMS_SingleCellVolt107;
    /**
     * 单体108电压
     */
    public BigDecimal BMS_SingleCellVolt108;
    /**
     * 单体109电压
     */
    public BigDecimal BMS_SingleCellVolt109;
    /**
     * 单体110电压
     */
    public BigDecimal BMS_SingleCellVolt110;
    /**
     * 单体111电压
     */
    public BigDecimal BMS_SingleCellVolt111;
    /**
     * 单体112电压
     */
    public BigDecimal BMS_SingleCellVolt112;

    /**
     * SOC电量
     */
    public BigDecimal BMS_BattSOC;

    /**
     * 车辆类型
     */
    public String vehicletype;
    /**
     * 车型名称
     */
    public String model;

    /**
     * 摄入时间
     */
    public String ingestionTime;
    /**
     * 摄入日期
     */
    public String ingestionDate;

    /**
     * 异常类型
     */
    String exceptionType = "无";

    /**
     * 经度
     */
    BigDecimal longitude;
    /**
     * 纬度
     */
    BigDecimal latitude;

    /**
     * 车型配置吗
     */
    public String model_code;

    List<BmsTrdStrategys> bmsTrdStrategies= new ArrayList<>();

    /**
     * 获取最大模组温度
     * @return
     */
    public Integer getMaxTempModuleData() {
        int maxTempModule = DefaultConstant.BMS_BattModuleTemp;
        return NumberUtil.max(this.getBMS_BattTempMax(),maxTempModule);
    }

    /**
     * 获取最小模组温度
     * @return
     */
    public Integer getMinTempModuleData() {
        int minTempModule =Integer.MAX_VALUE;
        minTempModule = NumberUtil.min(minTempModule, this.getBMS_BattTempMin());
        return minTempModule==Integer.MAX_VALUE?DefaultConstant.BMS_BattModuleTemp:minTempModule;
    }

    /**
     * 获取最大单体电压
     * @return
     */
    public BigDecimal getMaxSingleVolt() {
        BigDecimal maxVolt = DefaultConstant.BMS_SingleCellVolt;
        return NumberUtil.max(maxVolt,this.getBMS_CellMaxVolt());
    }

    /**
     * 获取最小单体电压
     * @return
     */
    public BigDecimal getMinSingleVolt() {
        BigDecimal minVolt =new BigDecimal(Integer.MAX_VALUE);
        minVolt= NumberUtil.min(minVolt,this.getBMS_CellVoltMin());
        return minVolt.compareTo(new BigDecimal(Integer.MAX_VALUE))==0?DefaultConstant.BMS_SingleCellVolt:minVolt;
    }

    /**
     * 获取模组最大温差
     * @return
     */
    public Integer getMaxTempDiff() {

        return NumberUtil.sub(this.getBMS_BattTempMax(), this.getBMS_BattTempMin()).intValue();
    }

    /**
     * 获取属性的Map结构集合
     * @return
     */
    public JSONObject getPropertiesMap() {
        Map<String,Object> map=new HashMap<>(0);
        List removeProperties=new ArrayList<>();
        removeProperties.add("bmsTrdStrategies");
        removeProperties.add("log");
        removeProperties.add("logger");
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field:fields) {
            try {
                if(!removeProperties.contains(field.getName())){
                    map.put(field.getName(),field.get(this));
                }
            } catch (IllegalAccessException e) {
                log.error("获取属性map集合失败",e);
                e.printStackTrace();
            }

        }
       return  mapToJsonObj(map,new JSONObject());
    }

    /**
     * 判断属性中是否有null值，有则返回true,无则返回false
     * @return
     */

    public boolean checkFieldNullValue(){
        List removeProperties=new ArrayList<>();
        removeProperties.add("bmsTrdStrategies");
        removeProperties.add("log");
        removeProperties.add("logger");
        Field[] fields = this.getClass().getDeclaredFields();
        try {
            for (Field field:fields) {
                if(!removeProperties.contains(field.getName())){
                    if(Objects.isNull(field.get(this))){
                        return true;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    private JSONObject mapToJsonObj(Map<String, Object> map, JSONObject resultJson) {
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            resultJson.put(key, map.get(key));
        }
        return resultJson;
    }

}
